package com.hlhj.funlike_business.activity;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.hlhj.funlike_business.Event.JpushEvent;
import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.fragment.MineFoodFragment;
import com.hlhj.funlike_business.activity.fragment.MineFoodFragment1;
import com.hlhj.funlike_business.activity.fragment.MineFragment;
import com.hlhj.funlike_business.activity.fragment.OrderFragment;
import com.hlhj.funlike_business.presenter.MainPresenter;
import com.hlhj.funlike_business.utils.PermisionUtils;
import com.hlhj.funlike_business.views.MainView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MainActivity extends BaseActivity<MainView,MainPresenter> implements MainView {
    private RadioGroup radiogroup;
    private RadioButton btn_oder;
    private List<Fragment> fragmentList = new ArrayList<>();
    private Fragment fragment;
    private FragmentTransaction transaction;
    private FragmentManager fragmentManager;
    private int index = 0;
    public static int foodtype_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PermisionUtils.verifyStoragePermissions(this);
        EventBus.getDefault().register(this);
    }

    @Override
    protected int getContentId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
        fragmentList = getFragments();
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        this.fragment = fragmentList.get(0);
        btn_oder.setChecked(true);

        transaction.replace(R.id.main_frag, this.fragment);
        transaction.commit();
    }

    public List<Fragment> getFragments() {
        fragmentList.add(new OrderFragment());
        fragmentList.add(new MineFoodFragment1());
        fragmentList.add(new MineFragment());
        return fragmentList;
    }

    @Override
    public void initView() {
        radiogroup=findViewById(R.id.radiogroup);
        btn_oder=findViewById(R.id.btn_oder);
    }

    @Override
    public void logicStart() {

    }

    @Override
    public void setListener() {
        radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.btn_oder:
                        Log.d("zy", checkedId + "");
                        changeUi(0);
                        break;
                    case R.id.btn_minefood:
                        Log.d("zy", checkedId + "");
                        changeUi(1);
                        break;
                    case R.id.btn_mine:
                        Log.d("zy", checkedId + "");
                        changeUi(2);
                        break;
                }
            }
        });
    }

    private void changeUi(int i) {
        if (index == i) {
            return;
        }
        fragmentManager = getSupportFragmentManager();
        transaction = fragmentManager.beginTransaction();
        //隐藏上一个界面
        transaction.hide(fragmentList.get(index));
        //判断是否添加界面
        if (!fragmentList.get(i).isAdded()) {
            transaction.add(R.id.main_frag, fragmentList.get(i)).show(fragmentList.get(i));
        } else {
            transaction.show(fragmentList.get(i));
        }
        transaction.commit();
        index = i;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        foodtype_id=0;
        EventBus.getDefault().unregister(this);
    }

    @NotNull
    @Override
    public MainView bindView() {
        return this;
    }

    @NotNull
    @Override
    public MainPresenter createPresenter() {
        return new MainPresenter();
    }

    private long time;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (System.currentTimeMillis() - time >2000){
                Toast.makeText(this, "再次点击退出", Toast.LENGTH_SHORT).show();
                time = System.currentTimeMillis();
                return true;
            }else {
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


}
